import Variable from "../constans/index";
import { ResponseData } from "../types/common";
import { HttpMethod } from "../types/enum";

export default class RequestHttp {
    static get<T>(url:string,params?:object){
        return request<T>(url,HttpMethod.GET,params)
    }
    static post () {}
    static put() {}
    static delete() {}
}
function request <T>(url:string,method:HttpMethod,data?:object){
    return new Promise<T>((resolve,reject)=>{
            wx.request<ResponseData<T>>({
            url:Variable.BASE_URL+url,
            method,
            data,
            success(res){
                if(res.data.code === 200){
                    // 此时认为成功
                    resolve(res.data.data)
                }else{
                    wx.showToast({
                        title:res.data.msg,
                        icon:'error'
                    })
                    reject(new Error(res.data.msg))
                }
            },
            fail(res){
                reject(new Error(res.errMsg))
            }
            // axios的get参数要传递params字段
            // axios的postbody参数要传递data字段
            // wx.request一律都叫data
        })
    })
     // wx.request不支持promise
}